<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    function foo (){
        var tmp = 10;
        return function(){
            console.log(++tmp);
        }
    }
    var fn1 = foo();
    fn1(10);
    fn1(1);


function counter(){
    var n = 0;
    return{
        count:function ()
        {
            return ++n;
        },
        reset:function ()
        {
            n=0;
            return n;
        }
    }
}
//c和d返回的对象，都有自己的作用域，互不影响
var c = counter(),d = counter();
console.log(c.count());
console.log(d.count());
console.log(c.reset());
console.log(c.count());
console.log(d.count());
console.log(d.count());
//先判断有无div，有就不创建，没有就创建
function fn(){
    var a;
    return function(){
        return a||(a = document.body.appendChild(document.createElement('div')));
    }
}
var f = fn();
f();
</script>
</html>